package com.wzs.easytobuy.handler;

import com.wzs.easytobuy.model.ProductType;
import com.wzs.easytobuy.service.IProductService;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author miaomiao
 * @version 1.0.0
 * @Description TODO
 * @createTime 2021-06-24- 16:00:00
 */
public class SystemListener implements ServletContextListener {

    /*因为listener是tomcat中的组件,在tomcat中使用spring的注解,tomcat不认识注解,就会导致tomcat运行失败
    @Resource
    private IProductService iProductService;*/

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("服务器缓存处理商品分类信息开始");
        ServletContext application = event.getServletContext();

        //获取spring容器中的IProductService对象
        IProductService productService = Objects.requireNonNull(WebApplicationContextUtils.getWebApplicationContext(event.getServletContext())).getBean(IProductService.class);

        Map<String, List<Map<String, List<ProductType>>>> map = productService.getAll();

        application.setAttribute("typeMap", map);

    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {

        System.out.println("服务器缓存处理商品分类信息结束");

    }
}
